﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Twodi.Aspect.Interfaces;
using Twodi.Interfaces;
using Microsoft.Xna.Framework;
using Twodi.Arguments;

namespace Twodi.Behavior.Implemented
{
    public class FollowBehavior<T, W> : AbstractBehavior<T>
        where T : IPositionAspect, ISizeAspect, IFollower<W>
        where W : IPositionAspect, ISizeAspect
    {
        public FollowBehavior(T component)
            : base(component)
        {

        }

        public override void Initialize()
        {
            return;

        }

        public override void Update(GameTime gameTime)
        {
            if (!Enabled) return;

            if (Component.Target != null)
            {
                W Target = Component.Target;
                Vector2 componentCenter = Component.PositionProperties.Position + Component.SizeProperties.Center;
                Vector2 targetCenter = Target.PositionProperties.Position + Target.SizeProperties.Center;

                Vector2 direction = targetCenter - componentCenter;
                direction.Normalize();
                FollowEventArgs e = new FollowEventArgs(direction);
                onFollow(e);
            }
        }

        public override void Draw(GameTime gameTime)
        {
            return;
        }

        public override bool Handle(Twodi.Input.KeyboardInput input)
        {
            throw new NotImplementedException();
        }

        public override bool Handle(Twodi.Input.MouseInput input)
        {
            throw new NotImplementedException();
        }

        public event EventHandler<FollowEventArgs> Follow;

        protected virtual void onFollow(FollowEventArgs e)
        {
            if (Follow != null)
            {
                Follow(this, e);
            }
        }
    }
}
